package com.sc.mapper;

import com.sc.pojo.BookField;
import com.sc.pojo.Comment;
import com.sc.pojo.Field;
import com.sc.pojo.User;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.List;

public interface UserMapper {
    //查找是否有该用户
    @Select("select * from user where userid=#{userid};")
    User selectById(String userid);
    //添加预定场地信息
    @Insert("insert into bookfield (fieldbookid,userid,fieldid,fieldname,bookday," +
            "starttime,endtime,fieldbookstate,submittime,orderstate) values (#{fieldbookid},#{userid},#{fieldid},#{fieldname},#{bookday}" +
            ",#{starttime},#{endtime},#{fieldbookstate},#{submittime},'未使用');")
    void addBookFieldMes(BookField bookFieldMes);
    //根据用户id场地号，时间查找预定信息
    @Select("select * from bookfield where userid=#{userid} and fieldid=#{fieldid} and bookday=#{bookday} and " +
            "starttime=#{starttime} and endtime=#{endtime};")
    BookField selectBy(BookField bookField);
    //根据用户ID查找所有预定场地信息
    @Select("select * from bookfield where userid=#{userid};")
    List<BookField> selectAllMyBookMes(String userid);

    //查找所有预定场地信息
    @Select("select * from bookfield ;")
    List<BookField> selectAllBookMes();

    //根据场地号查询所有评论
    @Select("select * from comment where fieldid=#{field};")
    List<Comment> selectAllComment(int fieldid);
    
    //添加评论信息
    @Insert("insert into comment (userid,fieldid,comment,time) values (#{userid},#{fieldid},#{comment},#{time});")
    void addComment(Comment comment);

    //根据UserId查询所有评论
    @Select("select * from comment where userid=#{userid};")
    List<Comment> selectAllCommentByUserId(String userid);

    //删除评论
    @Delete("delete from comment where userid=#{userid} and fieldid=#{fieldid} and time=#{time} ;")
    void deleteComment(Comment comment);

    //通过预约审核
    @Update("update bookfield set fieldbookstate = '预约成功' where fieldbookid=#{fieldbookid};")
    void updateFieldBookState(String fieldbookid);

    //拒绝预约审核
    @Update("update bookfield set fieldbookstate = '已拒绝' where fieldbookid=#{fieldbookid};")
    void refuseFieldBookState(String fieldbookid);

    //删除预约信息
    @Delete("delete from bookfield where fieldbookid=#{fieldbookid};")
    void deleteFieldBookMes(String fieldbookid);

    //根据预约时间查找预定信息

    List<BookField>  selectByDayTime(String bookday);

    //查询所有评论
    @Select("select * from comment;")
    List<Comment> adminSelectAllComment();

    //查询所有用户
    @Select("select * from user;")
    List<User> selectAllUser();

    //管理员更新用户信息

    void updateUserMes(User user);
    //根据userid删除用户
    @Delete("delete from user where userid = #{userid};")
    void deleteUserByUserId(String userid);

    //根据adminid查询adminname
    @Select("select adminname from admin where adminid = #{adminid};")
    String selectAdminName(String adminid);

    //判断预约当天 某个时间段是否重合
    @Select("SELECT * FROM bookfield where fieldid=#{fieldid} and bookday=#{bookday} and fieldbookstate ='预约成功' and starttime<=#{starttime} and endtime >#{starttime} ;")
    List<BookField> isConflict(BookField bookField);

    //判断预约当天 某个时间段是否重合
    @Select("SELECT * FROM bookfield where fieldid=#{fieldid} and bookday=#{bookday} and fieldbookstate ='预约成功' and starttime<#{endtime} and endtime >=#{endtime} ;")
    List<BookField> isConflict2(BookField bookField);
    
    //添加注册用户
    @Insert("insert into user (userid,username,password,sex,phone) values (#{userid},#{username},#{password},#{sex},#{phone});")
    void addUser(User user);



}
